package cn.otrue.patienthealthmanager.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import cn.otrue.patienthealthmanager.R;
import cn.otrue.patienthealthmanager.bean.DrugTypeModel;
import cn.otrue.patienthealthmanager.http.HttpServer;
import cn.otrue.patienthealthmanager.view.kankan.wheel.widget.ArrayWheelAdapter;
import cn.otrue.patienthealthmanager.view.kankan.wheel.widget.WheelView;

import com.google.gson.reflect.TypeToken;

/**
 * 药品选择Dialog
 * 
 * @author G
 * 
 */
public class DialogGrugActivity extends BaseActivity {

	private LinearLayout ll_drug_type;
	private int currentId;
	private WheelView wv_drug_drugname;
	private WheelView wv_drug_drugfrequency;
	private WheelView wv_drug_drugdose;
	private WheelView wv_drug_breakfast;
	private String[] drugnameArrs;
	private String[] drugfrequencyArrs;
	private String[] drugdoseArrs;
	private TextView tv_drug_window_cancel;
	private TextView tv_drug_window_center;
	private String[] breakfastArrs;
	private String drugStr;
	private List<DrugTypeModel> drugList;
	private List<Button> buttonList = new ArrayList<Button>();
	private Display display;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_drug_dialog);
		display = getWindowManager().getDefaultDisplay();
		getDate();
		initView();
	}

	private void getDate() {
		Intent intent = getIntent();
		drugStr = intent.getStringExtra("drugStr");
		drugnameArrs = new String[] { "" };
		drugfrequencyArrs = new String[] { "1", "2", "3", "4", "5", "6" };
		drugdoseArrs = new String[] { "0.5", "1.0", "1.5", "2.0", "2.5", "3.0" };
		breakfastArrs = new String[] { "餐前", "餐中", "餐后" };
		if (!TextUtils.isEmpty(drugStr)) {
			drugList = HttpServer.gson.fromJson(drugStr,
					new TypeToken<List<DrugTypeModel>>() {
					}.getType());

		}

	}

	private void initView() {

		ll_drug_type = (LinearLayout) findViewById(R.id.ll_drug_type);
		wv_drug_drugname = (WheelView) findViewById(R.id.wv_drug_drugname);
		wv_drug_drugfrequency = (WheelView) findViewById(R.id.wv_drug_drugfrequency);
		wv_drug_drugdose = (WheelView) findViewById(R.id.wv_drug_drugdose);
		wv_drug_breakfast = (WheelView) findViewById(R.id.wv_drug_breakfast);
		tv_drug_window_cancel = (TextView) findViewById(R.id.tv_drug_window_cancel);
		tv_drug_window_center = (TextView) findViewById(R.id.tv_drug_window_center);
		wv_drug_drugname.setOnClickListener(this);
		wv_drug_drugfrequency.setOnClickListener(this);
		wv_drug_drugdose.setOnClickListener(this);
		wv_drug_breakfast.setOnClickListener(this);

		tv_drug_window_cancel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				DialogGrugActivity.this.finish();
			}
		});
		tv_drug_window_center.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (currentId >= 0) {
					String drugid = drugList.get(currentId).getDrugmodellist()
							.get(wv_drug_drugname.getCurrentItem()).getDrugId();

					String drugname = drugnameArrs[wv_drug_drugname
							.getCurrentItem()];
					String drugfrequency = drugfrequencyArrs[wv_drug_drugfrequency
							.getCurrentItem()];
					String drugdose = drugdoseArrs[wv_drug_drugdose
							.getCurrentItem()];
					String breakfast = breakfastArrs[wv_drug_breakfast
							.getCurrentItem()];

					String[] arr = { drugid, drugname, drugfrequency, drugdose,
							breakfast };
					Intent intent = new Intent(DialogGrugActivity.this,
							DrugManagerActivity.class);
					intent.putExtra("results", arr);
					setResult(1, intent);
					finish();
				} else {
					Toast.makeText(DialogGrugActivity.this, "请选择药品", 0).show();
				}

			}
		});

		wv_drug_drugname.setViewAdapter(new ArrayWheelAdapter<String>(this,
				drugnameArrs));

		wv_drug_drugfrequency.setViewAdapter(new ArrayWheelAdapter<String>(
				this, drugfrequencyArrs));

		wv_drug_drugdose.setViewAdapter(new ArrayWheelAdapter<String>(this,
				drugdoseArrs));

		wv_drug_breakfast.setViewAdapter(new ArrayWheelAdapter<String>(this,
				breakfastArrs));

		wv_drug_drugname.setCurrentItem(0);
		wv_drug_drugfrequency.setCurrentItem(0);
		wv_drug_drugdose.setCurrentItem(0);
		wv_drug_breakfast.setCurrentItem(0);

		wv_drug_drugname.getCurrentItem();
		wv_drug_drugfrequency.getCurrentItem();
		wv_drug_drugdose.getCurrentItem();
		wv_drug_breakfast.getCurrentItem();

		android.widget.LinearLayout.LayoutParams params = new android.widget.LinearLayout.LayoutParams(
				(display.getWidth() / drugList.size() - 1),
				LayoutParams.WRAP_CONTENT);

		params.setMargins(3, 3, 3, 3);

		for (int i = 0; i < drugList.size(); i++) {

			Button button = new Button(this);
			if (i == 0) {
				button.setBackgroundResource(R.color.main);
				button.setTextColor(this.getResources().getColor(R.color.white));
			} else {
				button.setBackgroundResource(R.color.white);
				button.setTextColor(this.getResources().getColor(R.color.main));
			}

			button.setTag(i);
			button.setText(drugList.get(i).getDrugTypename());
			button.setOnClickListener(this);
			ll_drug_type.addView(button, params);
		}
		changeDrug(0);

	}

	@Override
	public void onClick(View v) {
		Object tag = v.getTag();
		if (tag != null) {
			currentId = Integer.parseInt(tag.toString());
			changeDrug(currentId);
			for (int i = 0; i < ll_drug_type.getChildCount(); i++) {
				if (currentId == i) {
					ll_drug_type.getChildAt(i).setBackgroundResource(
							R.color.main);
					((Button) ll_drug_type.getChildAt(i)).setTextColor(this
							.getResources().getColor(R.color.white));
				} else {
					ll_drug_type.getChildAt(i).setBackgroundResource(
							R.color.white);
					((Button) ll_drug_type.getChildAt(i)).setTextColor(this
							.getResources().getColor(R.color.main));
				}
			}
		}
	}

	private void changeDrug(int id) {

		int size = drugList.get(id).getDrugmodellist().size();
		drugnameArrs = new String[size];
		for (int i = 0; i < size; i++) {
			drugnameArrs[i] = drugList.get(id).getDrugmodellist().get(i)
					.getDrugName();
		}
		wv_drug_drugname.setViewAdapter(new ArrayWheelAdapter<String>(this,
				drugnameArrs));
	}
}
